#!/bin/bash

SCRIPT_DIR=$(cd $(dirname "$0"); pwd)

# 使用netstat命令结合grep查找占用8080端口的进程及其PID
PID=$(netstat -tulpn | grep :8080 | awk '{print $7}' | cut -d/ -f1)
# 检查是否有进程占用8080端口
if [[ -n "$PID" ]]; then
    echo "Process with PID $PID is occupying port 8080."
    # 使用kill命令终止该进程
    kill -9 "$PID" || { echo "Failed to stop process with PID $PID."; exit 1; }
    # 验证进程是否已停止
    if ! ps -p "$PID" > /dev/null; then
        echo "Process with PID $PID has been successfully terminated."
    else
        echo "Failed to terminate the process despite attempting to do so."
    fi
else
    echo "No process found occupying port 8080."
fi

cd $SCRIPT_DIR/..
git pull
mvn clean package -DskipTests

if [ $? -ne 0 ]; then
  echo 'mvn package failed' > $SCRIPT_DIR/build.log
  exit 1
fi

nohup java -jar $SCRIPT_DIR/../schedule-self-start/target/schedule-self-start-0.0.1-SNAPSHOT.jar &
